package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.BusinessSealtype;
import com.ruoyi.common.core.domain.entity.BusinessSys;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.domain.vo.BusinessSealTypeVo;
import com.ruoyi.system.mapper.BusinessSysMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BusinessSealtypeMapper;
import com.ruoyi.system.service.IBusinessSealtypeService;

/**
 * 业务系统对应印章Service业务层处理
 *
 * @author ess
 * @date 2021-12-03
 */
@Service
public class BusinessSealtypeServiceImpl implements IBusinessSealtypeService {
    @Autowired
    private BusinessSealtypeMapper businessSealtypeMapper;

    @Autowired
    private BusinessSysMapper businessSysMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询业务系统对应印章
     *
     * @param businessId 业务系统对应印章主键
     * @return 业务系统对应印章
     */
    @Override
    public BusinessSealtype selectBusinessSealtypeByBusinessId(String businessId) {
        return businessSealtypeMapper.selectBusinessSealtypeByBusinessId(businessId);
    }

    /**
     * 查询业务系统对应印章列表
     *
     * @param businessSealtype 业务系统对应印章
     * @return 业务系统对应印章
     */
    @Override
    public List<BusinessSealtype> selectBusinessSealtypeList(BusinessSealtype businessSealtype) {
        return businessSealtypeMapper.selectBusinessSealtypeList(businessSealtype);
    }

    /**
     * 新增业务系统对应印章
     *
     * @param businessSealtype 业务系统对应印章
     * @return 结果
     */
    @Override
    public int insertBusinessSealtype(BusinessSealtype businessSealtype) {
        return businessSealtypeMapper.insertBusinessSealtype(businessSealtype);
    }

    /**
     * 修改业务系统对应印章
     *
     * @param businessSealtype 业务系统对应印章
     * @return 结果
     */
    @Override
    public int updateBusinessSealtype(BusinessSealtype businessSealtype) {
        return businessSealtypeMapper.updateBusinessSealtype(businessSealtype);
    }

    /**
     * 批量删除业务系统对应印章
     *
     * @param businessIds 需要删除的业务系统对应印章主键
     * @return 结果
     */
    @Override
    public int deleteBusinessSealtypeByBusinessIds(String[] businessIds) {
        return businessSealtypeMapper.deleteBusinessSealtypeByBusinessIds(businessIds);
    }

    /**
     * 删除业务系统对应印章信息
     *
     * @param businessId 业务系统对应印章主键
     * @return 结果
     */
    @Override
    public int deleteBusinessSealtypeByBusinessId(String businessId) {
        return businessSealtypeMapper.deleteBusinessSealtypeByBusinessId(businessId);
    }

    @Override
    public List<String> selectBSListByBusinId(String businessId) {

        return businessSealtypeMapper.selectSealTypeListByBusinessId(businessId);
    }


    @Override
    public List<BusinessSealTypeVo> getBusinessSealTypeOptions() {
        ArrayList<BusinessSealTypeVo> businessSealTypeVos = new ArrayList<>();
//        1.获取所有的业务系统
        List<BusinessSys> businessSys = businessSysMapper.selectBusinessSysList(new BusinessSys());


        List<SysDictData> sealTypeDictData = sysDictDataMapper.selectDictDataByType("seal_type");

//        2.根据业务系统id 查找对应的印章类型         为1时查找全部类型  排除手写签名类型
        if (businessSys != null && businessSys.size() > 0) {
            for (BusinessSys businessSy : businessSys) {
                if (businessSy.getDataScope().equals("1")) {
                    ArrayList<BusinessSealTypeVo> businessSealTypeVoOnes = new ArrayList<>();
                    for (SysDictData sealTypeDictDatum : sealTypeDictData) {
                        if (!sealTypeDictDatum.getDictValue().equals("ESS001")) {
                            businessSealTypeVoOnes.add( new BusinessSealTypeVo(sealTypeDictDatum.getDictCode()+"",sealTypeDictDatum.getDictLabel(),sealTypeDictDatum.getDictValue()));
                        }
                    }
                    BusinessSealTypeVo businessSealTypeType1 = new BusinessSealTypeVo(businessSy.getbSysId(),businessSy.getbName(),businessSy.getbSysId(),businessSealTypeVoOnes);
                    businessSealTypeVos.add(businessSealTypeType1);
                } else if (businessSy.getDataScope().equals("2")) {
                    ArrayList<BusinessSealTypeVo> businessSealTypeVoTwos = new ArrayList<>();
                    List<String> sealTypes = businessSealtypeMapper.selectSealTypeListByBusinessId(businessSy.getbSysId());
                    if (sealTypes!=null&&sealTypes.size()>0) {
                        for (String sealType : sealTypes) {
                            if (!sealType.equals("ESS001")) {
                                for (SysDictData sealTypeDictDatum : sealTypeDictData) {

                                    if (sealTypeDictDatum.getDictValue().equals(sealType)) {
                                        businessSealTypeVoTwos.add(new BusinessSealTypeVo(sealTypeDictDatum.getDictCode()+"",sealTypeDictDatum.getDictLabel(),sealTypeDictDatum.getDictValue()));
                                    }
                                }
                            }

                        }

                        BusinessSealTypeVo businessSealTypeType2 = new BusinessSealTypeVo(businessSy.getbSysId(),businessSy.getbName(),businessSy.getbSysId(),businessSealTypeVoTwos);
                        businessSealTypeVos.add(businessSealTypeType2);

                    }

                }
            }

            return businessSealTypeVos;
        }

        return businessSealTypeVos;
    }
}
